package team.jaman.autosell.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import team.jaman.autosell.dao.LoggerMapper;
import team.jaman.autosell.dao.UsersMapper;
import team.jaman.autosell.entity.Logger;
import team.jaman.autosell.entity.Users;
import team.jaman.autosell.entity.UsersExample;
import team.jaman.autosell.entity.UsersExample.Criteria;
import team.jaman.autosell.service.UsersService;
import team.jaman.autosell.tools.CommonData;
import team.jaman.autosell.tools.PageData;
@Service
public class UsersServiceImpl implements UsersService {
	// 注入一个UsersMapper
	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private LoggerMapper loggerMapper;
	
	PageData<Users> pageData;
	
	/**
	 * 根据账号查询用户
	 */
	public Users loginUsers(Users record,Logger logger) {
		System.out.println(logger);
		UsersExample example = new UsersExample();
		
		example.createCriteria().andUserNameEqualTo(record.getUserName());
		List<Users> users = usersMapper.selectByExample(example);
		System.out.println(users);
		
		if (users.size() != 0) {
			logger.setUserId(users.get(0).getUserId());
			logger.setOperatorContent("loginUsers");
			logger.setErrorMsg("登录成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			System.out.println(logger);
			return users.get(0);
		}else {
			addUsers(record,logger);
			example.createCriteria().andUserNameEqualTo(record.getUserName());
			Users user0 = usersMapper.selectByExample(example).get(0);
			logger.setUserId(user0.getUserId());
			logger.setOperatorContent("loginUsers");
			logger.setErrorMsg("登录成功");
			logger.setEndTime(new Date());
			loggerMapper.insertSelective(logger);
			return user0;
		}
	}
	/**
	 * 增加用户
	 */
	public CommonData addUsers(Users record,Logger logger) {
		CommonData commonData = new CommonData();
		record.setUserStatus(1);
		int n = usersMapper.insert(record);
		
		if (n>0) {
			logger.setOperatorContent("addUser");
			logger.setErrorMsg("添加成功");
			logger.setEndTime(new Date());
			commonData.setMsg("添加成功");
			loggerMapper.insertSelective(logger);
		}else {
			logger.setOperatorContent("addUser");
			logger.setErrorMsg("添加失败");
			logger.setEndTime(new Date());
			commonData.setMsg("添加失败");
			loggerMapper.insertSelective(logger);
		}
		return commonData;
	}
	/**
	 * 查询用户
	 */
	public PageData<Users> selectUsers(int page, int limit, String keyword) {
		
		UsersExample example = new UsersExample();
		// pageHelper分页插件
		Page<Users> startPage = PageHelper.startPage(page, limit);
		// 模糊查询数据
		Criteria createCriteria = example.createCriteria();
		createCriteria.andUserNameLike("%" + keyword + "%");
		List<Users> data = usersMapper.selectByExample(example);
		// 总记录数
		long total = startPage.getTotal();
				
		PageData<Users> pageData = new PageData<Users>(data, (int) total, limit, page);
		pageData.setCode(0);
		pageData.setMsg("");
		return pageData;
	}
	/**
	 * 修改用户
	 */
	public CommonData updateByPrimaryKeySelective(Users record,Logger logger) {
		CommonData commonData = new CommonData();
		
		int n = usersMapper.updateByPrimaryKeySelective(record);
		System.out.println(record.getUserId());

		if (n>0) {
			logger.setOperatorContent("updateUser");
			logger.setErrorMsg("修改成功");
			logger.setEndTime(new Date());
			commonData.setMsg("修改成功");
			loggerMapper.insertSelective(logger);
		}else {
			logger.setOperatorContent("updateUser");
			logger.setErrorMsg("修改失败");
			logger.setEndTime(new Date());
			commonData.setMsg("修改失败");
			loggerMapper.insertSelective(logger);
			commonData.setMsg("修改失败");
		}
		return commonData;
	}
}
